<?php
/**
 * Display User Emails if an email box has set 
 * 
 * @author Xavier Vilain
 * @package Hive\Message
 * @license #hive-messaging#
 */
use Fox\Behaviors;
use Fox\Generator;
use Fox\Html\Button;
$xview = Xview::instance();
// First Check if an email box is set for the user
$mode = $xview->val('mode');
$mailers = User::current()->personalMailers();
if(!is_array($mailers) || count($mailers) == 0) {
	Fox\Message::Information(Msg(Hive::BUNDLE,Hive::MAILER_NOT_SETUP));
	$html = '<p>'.$xview->messages(true).'</p>';
	$button = new Button('setup','?mod=messages&act=mailbox&sac=connect');
	$button->label(Msg(Hive::BUNDLE,Hive::MAIL_BOX_CONNECT))
			->color(Button::COLOR_WARNING);
	$html .= '<p style="text-align:center">'.$button->display().'</p>';
	return $html;
}
// 1. Check for new emails
$xview->module(Generator::url('messages','synchronize',null,array('json' => false)));
// 2. Build Header
$messages = $xview->messages(true);

if($mode == 'widget') {
	$content = $xview->module(Generator::url('messages','list',null,
							array('status' => 'inbox',
								  'coche' => false,
								  'notajax' => true,
								  'mode' => $mode)));
	
	return '<div id="contentCol" class="dashboard-messages" style="border:0;margin-left:1px;">'.$content.'</div>';
} else {
	$content = $xview->module(Generator::url('messages','list',null,array('status' => 'inbox',
							'coche' => true,'splitted' => true,'notajax' => true)));
	
	$synchronization = new Behaviors(Behaviors::action(
		Generator::url(
			'messages',
			'synchronize',
			null,
			array('json' => true,'force'=>true)
		),false,null,null,false,'POST',null,null,null,true,Generator::ajaxReload('contentCol')
	));
	$newMessage = new Behaviors(Behaviors::action(
		Generator::url(
			'messages',
			'new',
			null
		),false,null,null,false,'POST',null,null,'contentCol'
	));
	$helpButton = new Behaviors(Behaviors::action(
		Generator::url(
			'messages',
			'help',
			null
		),true
	));
	$setupRule = new Behaviors(Behaviors::action(
		Generator::url(
			'messages',
			'banish',
			'modify_rule'
		),true
	));
	$rulesList = new Behaviors(Behaviors::action(
		Generator::url(
			'messages',
			'banish',
			'rules'
		),false,
		null,
		null,
		false,
		'POST',
		null,
		null,
		'contentCol'
	));
	$mailboxConfig = new Behaviors(Behaviors::action(
		Generator::url(
			'users',
			'inboxes',
			null
		),
		false,
		null,
		null,
		false,
		'POST',
		null,
		null,
		'contentCol'
	));
	$html = '<div class="webmail-toolbar" id="webmail-toolbar">
			<span class="webmail-header">'.Msg(Orx\Mailer\Messages::BUNDLE,Orx\Mailer\Messages::MESSAGES).'</span>
			<div class="messages-actions">
				<span id="checked-message-only" class="hide">
				<div class="btn-group">
					<button class="btn dropdown-toggle hastooltip" data-position="left" title="'.Msg(Hive::BUNDLE,Hive::PARSE_HAS_A_CONTACT).'" style="margin-left: -2px;" data-position="bottom" data-toggle="dropdown">
					<i class="icon-user"></i>
					<span class="caret"></span></button>
					<ul class="dropdown-menu">
	                  <li><a href="javascript:void(0)" id="act-parse-as-new">'.Msg(Hive::BUNDLE,Hive::PARSE_AS_A_NEW_CONTACT).'</a></li>
	                  <li><a href="javascript:void(0)" id="act-parse-as-existing">'.Msg(Hive::BUNDLE,Hive::PARSE_AS_AN_EXISTING_CONTACT).'</a></li>
	                  <li><a href="javascript:void(0)" id="act-auto-parse">'.Msg(Hive::BUNDLE,Hive::PARSE_AUTO).'</a></li>
	                </ul>
                </div>
                
                <div class="btn-group">
				<button id="act-archive" title="'.Msg(Orx\Mailer\Messages::BUNDLE,Orx\Mailer\Messages::ARCHIVE).'" class="btn hastooltip " href="#" style="margin-left: -2px;" data-position="bottom" >
					<i class="icon-folder-close"></i>
				</button>
				</div>
                <div class="btn-group">
				<button id="act-move-to" title="'.Msg(Orx\Mailer\Messages::BUNDLE,Orx\Mailer\Messages::MOVE_TO).'" class="btn hastooltip " href="#" style="margin-left: -2px;" data-position="bottom" >
					<i class="icon-folder-open"></i>
				</button>
				</div>
				<div class="btn-group">
					<button class="btn dropdown-toggle hastooltip" title="'.Msg(Hive::BUNDLE,Hive::BAN_MESSAGE).'" style="margin-left: -2px;" data-position="bottom" data-toggle="dropdown">
					<i class="icon-ban-circle"></i>
					<span class="caret"></span></button>
					<ul class="dropdown-menu">
	                  <li><a href="javascript:void(0)" id="act-banish">'.Msg(Orx\Mailer\Messages::BUNDLE,Orx\Mailer\Messages::BANISH).'</a></li>
	                  <li><a href="javascript:void(0)" class="'.$setupRule->serialize(array(),'setuprule').'">'.Msg(Orx\Mailer\Messages::BUNDLE,Orx\Mailer\Messages::SETUP_A_RULE).'</a></li>
	                </ul>
                </div>
                <div class="btn-group">
					<button id="act-delete" title="'.Msg(Orx\Mailer\Messages::BUNDLE,Orx\Mailer\Messages::DELETE).'" class="btn hastooltip " style="margin-left: -2px;" data-position="bottom" >
						<i class="icon-trash"></i>
					</button>
				</div>
				</span>
				<a style="margin-left:45px" title="'.Msg(Orx\Mailer\Messages::BUNDLE,Orx\Mailer\Messages::SYNCHRONIZE).'" class="btn hastooltip '.$synchronization->serialize(array(),'sync').'" href="#" style="margin-left: -2px;" data-position="bottom" >
					<i class="icon-repeat"></i>
				</a>
				<a class="btn btn-warning '.$newMessage->serialize(array(),'sync').'" href="#" style="margin-left: -2px;" data-position="bottom" >
					'.Msg(Orx\Mailer\Messages::BUNDLE,Orx\Mailer\Messages::NEW_MESSAGE).'
				</a>
				<a class="btn '.$helpButton->serialize(array(),'help').'" href="#" style="margin-left: -2px;" data-position="bottom" >
					'.Msg(Hive::BUNDLE,Hive::HELP).'
				</a>
			</div>
			
			<div class="params" style="position: absolute;">
				<div class="btn-group">
					<button class="btn dropdown-toggle hastooltip" title="'.Msg(Orx\Mailer\Messages::BUNDLE,Orx\Mailer\Messages::PARAMETERS).'" style="margin-left: -2px;" data-position="bottom" data-toggle="dropdown">
					<i class="icon-wrench"></i>
					<span class="caret"></span></button>
					<ul class="dropdown-menu pull-right">
	                  <li><a href="javascript:void(0)" class="'.$mailboxConfig->serialize(array(),'config').'">'.Msg(Orx\Mailer\Messages::BUNDLE,Orx\Mailer\Messages::CONFIGURATION).'</a></li>
	                  <li><a href="javascript:void(0)" class="'.$rulesList->serialize(array(),'rules').'">'.Msg(Orx\Mailer\Messages::BUNDLE,Orx\Mailer\Messages::EXCLUSION_RULES).'</a></li>
	                </ul>
				</div>
			</div>
		</div>';

echo $html;	
\Fox\Ressources::instance()->register(new \Fox\Ressources\Script(_ASSETS_.'js/mailbox-toolbar.js'));
echo \Fox\Ressources::instance()->dump();

$page = 'inbox';
$leftColumnObj = $xview->module(Generator::url('messages','left'));
$leftColumnObj->item($page)->selected();
$leftColumn = $leftColumnObj->display();
?>
<div class="leftColContainer">
	<div class="leftColumn" style="margin:0;padding:0">
		<?php echo $leftColumn;?>
	</div>
</div>
<div class="hasLeftCol wnb" style="border:0;margin-left: 191px;" id="contentCol">
	<?php echo $messages.$content;?>
</div>
<?php } ?>